package org.jivesoftware.smackx.packet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smack.util.StringUtils;

public class VCard extends IQ
{
  private Map<String, String> homePhones = new HashMap();
  private Map<String, String> workPhones = new HashMap();
  private Map<String, String> homeAddr = new HashMap();
  private Map<String, String> workAddr = new HashMap();
  private String firstName;
  private String lastName;
  private String middleName;
  private String emailHome;
  private String emailWork;
  private String organization;
  private String organizationUnit;
  private String photoMimeType;
  private String photoBinval;
  private Map<String, String> otherSimpleFields = new HashMap();
  private Map<String, String> otherUnescapableFields = new HashMap();

  public String getField(String paramString)
  {
    return (String)this.otherSimpleFields.get(paramString);
  }

  public void setField(String paramString1, String paramString2)
  {
    setField(paramString1, paramString2, false);
  }

  public void setField(String paramString1, String paramString2, boolean paramBoolean)
  {
    if (!paramBoolean)
      this.otherSimpleFields.put(paramString1, paramString2);
    else
      this.otherUnescapableFields.put(paramString1, paramString2);
  }

  public String getFirstName()
  {
    return this.firstName;
  }

  public void setFirstName(String paramString)
  {
    this.firstName = paramString;
    updateFN();
  }

  public String getLastName()
  {
    return this.lastName;
  }

  public void setLastName(String paramString)
  {
    this.lastName = paramString;
    updateFN();
  }

  public String getMiddleName()
  {
    return this.middleName;
  }

  public void setMiddleName(String paramString)
  {
    this.middleName = paramString;
    updateFN();
  }

  public String getNickName()
  {
    return (String)this.otherSimpleFields.get("NICKNAME");
  }

  public void setNickName(String paramString)
  {
    this.otherSimpleFields.put("NICKNAME", paramString);
  }

  public String getEmailHome()
  {
    return this.emailHome;
  }

  public void setEmailHome(String paramString)
  {
    this.emailHome = paramString;
  }

  public String getEmailWork()
  {
    return this.emailWork;
  }

  public void setEmailWork(String paramString)
  {
    this.emailWork = paramString;
  }

  public String getJabberId()
  {
    return (String)this.otherSimpleFields.get("JABBERID");
  }

  public void setJabberId(String paramString)
  {
    this.otherSimpleFields.put("JABBERID", paramString);
  }

  public String getOrganization()
  {
    return this.organization;
  }

  public void setOrganization(String paramString)
  {
    this.organization = paramString;
  }

  public String getOrganizationUnit()
  {
    return this.organizationUnit;
  }

  public void setOrganizationUnit(String paramString)
  {
    this.organizationUnit = paramString;
  }

  public String getAddressFieldHome(String paramString)
  {
    return (String)this.homeAddr.get(paramString);
  }

  public void setAddressFieldHome(String paramString1, String paramString2)
  {
    this.homeAddr.put(paramString1, paramString2);
  }

  public String getAddressFieldWork(String paramString)
  {
    return (String)this.workAddr.get(paramString);
  }

  public void setAddressFieldWork(String paramString1, String paramString2)
  {
    this.workAddr.put(paramString1, paramString2);
  }

  public void setPhoneHome(String paramString1, String paramString2)
  {
    this.homePhones.put(paramString1, paramString2);
  }

  public String getPhoneHome(String paramString)
  {
    return (String)this.homePhones.get(paramString);
  }

  public void setPhoneWork(String paramString1, String paramString2)
  {
    this.workPhones.put(paramString1, paramString2);
  }

  public String getPhoneWork(String paramString)
  {
    return (String)this.workPhones.get(paramString);
  }

  public void setAvatar(URL paramURL)
  {
    byte[] arrayOfByte = new byte[0];
    try
    {
      arrayOfByte = getBytes(paramURL);
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
    setAvatar(arrayOfByte);
  }

  public void removeAvatar()
  {
    this.photoBinval = null;
    this.photoMimeType = null;
  }

  public void setAvatar(byte[] paramArrayOfByte)
  {
    setAvatar(paramArrayOfByte, "image/jpeg");
  }

  public void setAvatar(byte[] paramArrayOfByte, String paramString)
  {
    if (paramArrayOfByte == null)
    {
      removeAvatar();
      return;
    }
    String str = StringUtils.encodeBase64(paramArrayOfByte);
    setAvatar(str, paramString);
  }

  public void setAvatar(String paramString1, String paramString2)
  {
    this.photoBinval = paramString1;
    this.photoMimeType = paramString2;
  }

  public byte[] getAvatar()
  {
    if (this.photoBinval == null)
      return null;
    return StringUtils.decodeBase64(this.photoBinval);
  }

  public String getAvatarMimeType()
  {
    return this.photoMimeType;
  }

  public static byte[] getBytes(URL paramURL)
    throws IOException
  {
    String str = paramURL.getPath();
    File localFile = new File(str);
    if (localFile.exists())
      return getFileBytes(localFile);
    return null;
  }

  private static byte[] getFileBytes(File paramFile)
    throws IOException
  {
    BufferedInputStream localBufferedInputStream = null;
    try
    {
      localBufferedInputStream = new BufferedInputStream(new FileInputStream(paramFile));
      int i = (int)paramFile.length();
      byte[] arrayOfByte1 = new byte[i];
      int j = localBufferedInputStream.read(arrayOfByte1);
      if (j != arrayOfByte1.length)
        throw new IOException("Entire file not read");
      byte[] arrayOfByte2 = arrayOfByte1;
      return arrayOfByte2;
    }
    finally
    {
      if (localBufferedInputStream != null)
        localBufferedInputStream.close();
    }
  }

  public String getAvatarHash()
  {
    byte[] arrayOfByte = getAvatar();
    if (arrayOfByte == null)
      return null;
    MessageDigest localMessageDigest;
    try
    {
      localMessageDigest = MessageDigest.getInstance("SHA-1");
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      localNoSuchAlgorithmException.printStackTrace();
      return null;
    }
    localMessageDigest.update(arrayOfByte);
    return StringUtils.encodeHex(localMessageDigest.digest());
  }

  private void updateFN()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if (this.firstName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.firstName)).append(' ');
    if (this.middleName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.middleName)).append(' ');
    if (this.lastName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.lastName));
    setField("FN", localStringBuilder.toString());
  }

  public void save(Connection paramConnection)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, true);
    setType(IQ.Type.SET);
    setFrom(paramConnection.getUser());
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(getPacketID()));
    paramConnection.sendPacket(this);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from server on status set.");
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
  }

  public void load(Connection paramConnection)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, true);
    setFrom(paramConnection.getUser());
    doLoad(paramConnection, paramConnection.getUser());
  }

  public void load(Connection paramConnection, String paramString)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, false);
    setTo(paramString);
    doLoad(paramConnection, paramString);
  }

  private void doLoad(Connection paramConnection, String paramString)
    throws XMPPException
  {
    setType(IQ.Type.GET);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(getPacketID()));
    paramConnection.sendPacket(this);
    VCard localVCard = null;
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (localPacket == null)
    {
      String str = "Timeout getting VCard information";
      throw new XMPPException(str, new XMPPError(XMPPError.Condition.request_timeout, str));
    }
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
    try
    {
      localVCard = (VCard)localPacket;
    }
    catch (ClassCastException localClassCastException)
    {
      System.out.println("No VCard for " + paramString);
      return;
    }
    copyFieldsFrom(localVCard);
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    new VCardWriter(localStringBuilder).write();
    return localStringBuilder.toString();
  }

  private void copyFieldsFrom(VCard paramVCard)
  {
    Field[] arrayOfField1 = VCard.class.getDeclaredFields();
    for (Field localField : arrayOfField1)
      if ((localField.getDeclaringClass() == VCard.class) && (!Modifier.isFinal(localField.getModifiers())))
        try
        {
          localField.setAccessible(true);
          localField.set(this, localField.get(paramVCard));
        }
        catch (IllegalAccessException localIllegalAccessException)
        {
          throw new RuntimeException("This cannot happen:" + localField, localIllegalAccessException);
        }
  }

  private void checkAuthenticated(Connection paramConnection, boolean paramBoolean)
  {
    if (paramConnection == null)
      throw new IllegalArgumentException("No connection was provided");
    if (!paramConnection.isAuthenticated())
      throw new IllegalArgumentException("Connection is not authenticated");
    if ((paramBoolean) && (paramConnection.isAnonymous()))
      throw new IllegalArgumentException("Connection cannot be anonymous");
  }

  private boolean hasContent()
  {
    return (hasNameField()) || (hasOrganizationFields()) || (this.emailHome != null) || (this.emailWork != null) || (this.otherSimpleFields.size() > 0) || (this.otherUnescapableFields.size() > 0) || (this.homeAddr.size() > 0) || (this.homePhones.size() > 0) || (this.workAddr.size() > 0) || (this.workPhones.size() > 0) || (this.photoBinval != null);
  }

  private boolean hasNameField()
  {
    return (this.firstName != null) || (this.lastName != null) || (this.middleName != null);
  }

  private boolean hasOrganizationFields()
  {
    return (this.organization != null) || (this.organizationUnit != null);
  }

  public boolean equals(Object paramObject)
  {
    if (this == paramObject)
      return true;
    if ((paramObject == null) || (getClass() != paramObject.getClass()))
      return false;
    VCard localVCard = (VCard)paramObject;
    if (this.emailHome != null ? !this.emailHome.equals(localVCard.emailHome) : localVCard.emailHome != null)
      return false;
    if (this.emailWork != null ? !this.emailWork.equals(localVCard.emailWork) : localVCard.emailWork != null)
      return false;
    if (this.firstName != null ? !this.firstName.equals(localVCard.firstName) : localVCard.firstName != null)
      return false;
    if (!this.homeAddr.equals(localVCard.homeAddr))
      return false;
    if (!this.homePhones.equals(localVCard.homePhones))
      return false;
    if (this.lastName != null ? !this.lastName.equals(localVCard.lastName) : localVCard.lastName != null)
      return false;
    if (this.middleName != null ? !this.middleName.equals(localVCard.middleName) : localVCard.middleName != null)
      return false;
    if (this.organization != null ? !this.organization.equals(localVCard.organization) : localVCard.organization != null)
      return false;
    if (this.organizationUnit != null ? !this.organizationUnit.equals(localVCard.organizationUnit) : localVCard.organizationUnit != null)
      return false;
    if (!this.otherSimpleFields.equals(localVCard.otherSimpleFields))
      return false;
    if (!this.workAddr.equals(localVCard.workAddr))
      return false;
    if (this.photoBinval != null ? !this.photoBinval.equals(localVCard.photoBinval) : localVCard.photoBinval != null)
      return false;
    return this.workPhones.equals(localVCard.workPhones);
  }

  public int hashCode()
  {
    int i = this.homePhones.hashCode();
    i = 29 * i + this.workPhones.hashCode();
    i = 29 * i + this.homeAddr.hashCode();
    i = 29 * i + this.workAddr.hashCode();
    i = 29 * i + (this.firstName != null ? this.firstName.hashCode() : 0);
    i = 29 * i + (this.lastName != null ? this.lastName.hashCode() : 0);
    i = 29 * i + (this.middleName != null ? this.middleName.hashCode() : 0);
    i = 29 * i + (this.emailHome != null ? this.emailHome.hashCode() : 0);
    i = 29 * i + (this.emailWork != null ? this.emailWork.hashCode() : 0);
    i = 29 * i + (this.organization != null ? this.organization.hashCode() : 0);
    i = 29 * i + (this.organizationUnit != null ? this.organizationUnit.hashCode() : 0);
    i = 29 * i + this.otherSimpleFields.hashCode();
    i = 29 * i + (this.photoBinval != null ? this.photoBinval.hashCode() : 0);
    return i;
  }

  public String toString()
  {
    return getChildElementXML();
  }

  private static abstract interface ContentBuilder
  {
    public abstract void addTagContent();
  }

  private class VCardWriter
  {
    private final StringBuilder sb;

    VCardWriter(StringBuilder arg2)
    {
      Object localObject;
      this.sb = localObject;
    }

    public void write()
    {
      appendTag("vCard", "xmlns", "vcard-temp", VCard.this.hasContent(), new VCard.ContentBuilder()
      {
        public void addTagContent()
        {
          VCard.VCardWriter.this.buildActualContent();
        }
      });
    }

    private void buildActualContent()
    {
      if (VCard.this.hasNameField())
        appendN();
      appendOrganization();
      appendGenericFields();
      appendPhoto();
      appendEmail(VCard.this.emailWork, "WORK");
      appendEmail(VCard.this.emailHome, "HOME");
      appendPhones(VCard.this.workPhones, "WORK");
      appendPhones(VCard.this.homePhones, "HOME");
      appendAddress(VCard.this.workAddr, "WORK");
      appendAddress(VCard.this.homeAddr, "HOME");
    }

    private void appendPhoto()
    {
      if (VCard.this.photoBinval == null)
        return;
      appendTag("PHOTO", true, new VCard.ContentBuilder()
      {
        public void addTagContent()
        {
          VCard.VCardWriter.this.appendTag("BINVAL", VCard.this.photoBinval);
          VCard.VCardWriter.this.appendTag("TYPE", StringUtils.escapeForXML(VCard.this.photoMimeType));
        }
      });
    }

    private void appendEmail(final String paramString1, final String paramString2)
    {
      if (paramString1 != null)
        appendTag("EMAIL", true, new VCard.ContentBuilder()
        {
          public void addTagContent()
          {
            VCard.VCardWriter.this.appendEmptyTag(paramString2);
            VCard.VCardWriter.this.appendEmptyTag("INTERNET");
            VCard.VCardWriter.this.appendEmptyTag("PREF");
            VCard.VCardWriter.this.appendTag("USERID", StringUtils.escapeForXML(paramString1));
          }
        });
    }

    private void appendPhones(Map<String, String> paramMap, final String paramString)
    {
      Iterator localIterator = paramMap.entrySet().iterator();
      while (localIterator.hasNext())
      {
        final Map.Entry localEntry = (Map.Entry)localIterator.next();
        appendTag("TEL", true, new VCard.ContentBuilder()
        {
          public void addTagContent()
          {
            VCard.VCardWriter.this.appendEmptyTag(localEntry.getKey());
            VCard.VCardWriter.this.appendEmptyTag(paramString);
            VCard.VCardWriter.this.appendTag("NUMBER", StringUtils.escapeForXML((String)localEntry.getValue()));
          }
        });
      }
    }

    private void appendAddress(final Map<String, String> paramMap, final String paramString)
    {
      if (paramMap.size() > 0)
        appendTag("ADR", true, new VCard.ContentBuilder()
        {
          public void addTagContent()
          {
            VCard.VCardWriter.this.appendEmptyTag(paramString);
            Iterator localIterator = paramMap.entrySet().iterator();
            while (localIterator.hasNext())
            {
              Map.Entry localEntry = (Map.Entry)localIterator.next();
              VCard.VCardWriter.this.appendTag((String)localEntry.getKey(), StringUtils.escapeForXML((String)localEntry.getValue()));
            }
          }
        });
    }

    private void appendEmptyTag(Object paramObject)
    {
      this.sb.append('<').append(paramObject).append("/>");
    }

    private void appendGenericFields()
    {
      Iterator localIterator = VCard.this.otherSimpleFields.entrySet().iterator();
      Map.Entry localEntry;
      while (localIterator.hasNext())
      {
        localEntry = (Map.Entry)localIterator.next();
        appendTag(((String)localEntry.getKey()).toString(), StringUtils.escapeForXML((String)localEntry.getValue()));
      }
      localIterator = VCard.this.otherUnescapableFields.entrySet().iterator();
      while (localIterator.hasNext())
      {
        localEntry = (Map.Entry)localIterator.next();
        appendTag(((String)localEntry.getKey()).toString(), (String)localEntry.getValue());
      }
    }

    private void appendOrganization()
    {
      if (VCard.this.hasOrganizationFields())
        appendTag("ORG", true, new VCard.ContentBuilder()
        {
          public void addTagContent()
          {
            VCard.VCardWriter.this.appendTag("ORGNAME", StringUtils.escapeForXML(VCard.this.organization));
            VCard.VCardWriter.this.appendTag("ORGUNIT", StringUtils.escapeForXML(VCard.this.organizationUnit));
          }
        });
    }

    private void appendN()
    {
      appendTag("N", true, new VCard.ContentBuilder()
      {
        public void addTagContent()
        {
          VCard.VCardWriter.this.appendTag("FAMILY", StringUtils.escapeForXML(VCard.this.lastName));
          VCard.VCardWriter.this.appendTag("GIVEN", StringUtils.escapeForXML(VCard.this.firstName));
          VCard.VCardWriter.this.appendTag("MIDDLE", StringUtils.escapeForXML(VCard.this.middleName));
        }
      });
    }

    private void appendTag(String paramString1, String paramString2, String paramString3, boolean paramBoolean, VCard.ContentBuilder paramContentBuilder)
    {
      this.sb.append('<').append(paramString1);
      if (paramString2 != null)
        this.sb.append(' ').append(paramString2).append('=').append('\'').append(paramString3).append('\'');
      if (paramBoolean)
      {
        this.sb.append('>');
        paramContentBuilder.addTagContent();
        this.sb.append("</").append(paramString1).append(">\n");
      }
      else
      {
        this.sb.append("/>\n");
      }
    }

    private void appendTag(String paramString, boolean paramBoolean, VCard.ContentBuilder paramContentBuilder)
    {
      appendTag(paramString, null, null, paramBoolean, paramContentBuilder);
    }

    private void appendTag(String paramString1, final String paramString2)
    {
      if (paramString2 == null)
        return;
      VCard.ContentBuilder local8 = new VCard.ContentBuilder()
      {
        public void addTagContent()
        {
          VCard.VCardWriter.this.sb.append(paramString2.trim());
        }
      };
      appendTag(paramString1, true, local8);
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.packet.VCard
 * JD-Core Version:    0.6.1
 */